package com.orhon.smartcampus.modules.user.mutation;


import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.student.entity.SInformation;
import com.orhon.smartcampus.modules.user.dao.UsersDao;
import com.orhon.smartcampus.modules.user.entity.Users;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class DeleteUserMutation implements DataFetcher {

    @Autowired
    UsersDao usersDao;

    @Override
    @Transactional(readOnly = false)
    public Object get(DataFetchingEnvironment env) throws Exception {
        if (env.getArgument("id") != null) {
            List<Integer> ids = env.getArgument("id");
            ids.forEach(id -> {
                Users item = usersDao.get(Users.class, Long.valueOf(id));
                item.setDeleted_at(new Date());
                try {
                    usersDao.update(item);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            return Result.ok();
        }
        return Result.error();
    }
}
